/**
  ******************************************************************************
  * @file    User/tk_slip.c
  * @author  JUSHENG Application Team
  * @version V1.0.0
  * @date    05-18-2022
  * @brief   TK slip program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2022 JUSHENG</center></h2>
  *
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "tk_define.h"

/** @addtogroup Template_Project
  * @{
  */
  

#if TK_SWD_DEBUG_EN

#define TK_SLIP_STRIP_EN  0
#define TK_SLIP_RING_EN   0

//u8  SlipLoosenCnt_Strip;
//u8  SlipLoosenCnt;
extern u16 tk_swd_buf[TK_NUM];


#if TK_SLIP_STRIP_EN

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

#define STRIP_TK_NUM    4
#define STRIP_TK_THRE   150
u8  strip_num[STRIP_TK_NUM]={1,2,3,4};
u8  strip_pos[STRIP_TK_NUM +1] = {1,34,68,100,132};

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

void tk_slip_strip(u16*  pslipvalue)
{
    u8 i;
    u16 strip_tk_buf[STRIP_TK_NUM];
    u16 strip_temp;
    
    for(i = 0; i < STRIP_TK_NUM; i++) 
    { 
        strip_tk_buf[i] = tk_swd_buf[ strip_num[i] ];
    } 

    u16 strip_max = 0;
    u8 strip_index = 0;
    for(i = 0; i < STRIP_TK_NUM; i++)  
    { 
        if(strip_tk_buf[i] > strip_max)
        {
            strip_max = strip_tk_buf[i];
            strip_index = i;
        }
    } 

    if(strip_tk_buf[strip_index] >= STRIP_TK_THRE)  
    {  
        //SlipLoosenCnt_Strip = 20;

     
        if(strip_index == 0) 
        {
            strip_temp = ((strip_tk_buf[strip_index] * strip_pos[strip_index]) + (strip_tk_buf[strip_index+1] * strip_pos[strip_index+1]) + 
                         ((strip_tk_buf[strip_index] + strip_tk_buf[strip_index+1])/2))/
                          (strip_tk_buf[strip_index] + strip_tk_buf[strip_index+1]);
        }
        else 
        if(strip_index == (STRIP_TK_NUM-1)) 
        {
            strip_temp = ((strip_tk_buf[strip_index] * strip_pos[strip_index] + strip_tk_buf[strip_index-1] * strip_pos[strip_index-1]) + 
                         ((strip_tk_buf[strip_index] + strip_tk_buf[strip_index-1])/2))/
                          (strip_tk_buf[strip_index] + strip_tk_buf[strip_index-1]);
        }
        else 
        if(strip_index != 0 || strip_index != (STRIP_TK_NUM-1)) 
        {
            strip_temp = ((strip_tk_buf[strip_index-1] * strip_pos[strip_index-1] + strip_tk_buf[strip_index] * strip_pos[strip_index] + strip_tk_buf[strip_index+1] * strip_pos[strip_index+1])+
                         ((strip_tk_buf[strip_index-1] + strip_tk_buf[strip_index] + strip_tk_buf[strip_index+1])/2))/
                          (strip_tk_buf[strip_index-1] + strip_tk_buf[strip_index] + strip_tk_buf[strip_index+1]); 
        }

        if(*pslipvalue)
        {
            *pslipvalue = (strip_temp + *pslipvalue) / 2;
        }
        else
        {
            *pslipvalue = strip_temp;
        }
    }
    else
    {
        /*if(SlipLoosenCnt_Strip)
        {
            SlipLoosenCnt_Strip--;
        }
        else
        {
            //SlipValue = 0;
        }*/
    }
} 
#endif //TK_SLIP_STRIP_EN


#if TK_SLIP_RING_EN

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

#define RING_TK_NUM    4
#define RING_TK_THRE   150
u8  ring_num[RING_TK_NUM]={7,8,5,6};
//u8  ring_pos[RING_TK_NUM+1] = {1,25,50,75,100};
u8  ring_pos[RING_TK_NUM+1] = {1,25,50,75,125};

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void tk_slip_ring(u16*  pslipvalue)
{

    u8 i;
    u16 ring_tk_buf[STRIP_TK_NUM];
    u16 ring_temp;
    
    for(i = 0; i < RING_TK_NUM; i++) 
    { 
        ring_tk_buf[i] = tk_swd_buf[ ring_num[i] ];
    } 
     

    u16 ring_max = 0;
    u8 ring_index = 0;
    for(i=0; i < RING_TK_NUM; i++)  
    { 
        if(ring_tk_buf[i] > ring_max)
        {
            ring_max = ring_tk_buf[i];
            ring_index = i;
        }
    } 
    
    if(ring_tk_buf[ring_index] >= RING_TK_THRE)  
    {  

        if(ring_index == 0)
        {
            if(ring_tk_buf[ring_index +1] > ring_tk_buf[ring_index + RING_TK_NUM -1])// 0-->1
            {
                ring_temp = ((ring_tk_buf[ring_index] * ring_pos[ring_index]) + (ring_tk_buf[ring_index+1] * ring_pos[ring_index+1]) + 
                            ((ring_tk_buf[ring_index] + ring_tk_buf[ring_index+1])/2))/
                             (ring_tk_buf[ring_index] + ring_tk_buf[ring_index+1]);
            }
            if(ring_tk_buf[ring_index + 1] < ring_tk_buf[ring_index + RING_TK_NUM -1])// 0-->3
            {
                ring_temp = ((ring_tk_buf[ring_index] * ring_pos[ring_index + RING_TK_NUM] + ring_tk_buf[ring_index + RING_TK_NUM -1] * ring_pos[ring_index + RING_TK_NUM -1]) + 
                            ((ring_tk_buf[ring_index] + ring_tk_buf[ring_index + RING_TK_NUM -1])/2))/
                             (ring_tk_buf[ring_index] + ring_tk_buf[ring_index + RING_TK_NUM -1]);
            }
        }

        if(ring_index != 0 || ring_index != (RING_TK_NUM-1)) 
        {
            ring_temp = ((ring_tk_buf[ring_index-1] * ring_pos[ring_index-1] + ring_tk_buf[ring_index] * ring_pos[ring_index] + ring_tk_buf[ring_index+1] * ring_pos[ring_index+1])+
                        ((ring_tk_buf[ring_index-1] + ring_tk_buf[ring_index] + ring_tk_buf[ring_index+1])/2))/
                         (ring_tk_buf[ring_index-1] + ring_tk_buf[ring_index] + ring_tk_buf[ring_index+1]);  
        }

        if(ring_index == (RING_TK_NUM -1))
        {
            if(ring_tk_buf[ring_index-3] < ring_tk_buf[ring_index-1])// 2-->3
            {
                ring_temp = ((ring_tk_buf[ring_index-1] * ring_pos[ring_index-1] + ring_tk_buf[ring_index] * ring_pos[ring_index])+
                            ((ring_tk_buf[ring_index-1] + ring_tk_buf[ring_index])/2))/
                             (ring_tk_buf[ring_index-1] + ring_tk_buf[ring_index]);  
            }
            else// 3-->0
            {
                ring_temp = ((ring_tk_buf[ring_index] * ring_pos[ring_index] + ring_tk_buf[ring_index-(RING_TK_NUM -1)] * ring_pos[ring_index+1]) + 
                            ((ring_tk_buf[ring_index-(RING_TK_NUM -1)] + ring_tk_buf[ring_index])/2))/
                             (ring_tk_buf[ring_index-(RING_TK_NUM -1)] + ring_tk_buf[ring_index]);
            }
        }

        if(*pslipvalue)
        {
            *pslipvalue = (ring_temp + *pslipvalue) / 2;
        }
        else
        {
            *pslipvalue = ring_temp;
        }
        
    }
} 


#endif //TK_SLIP_RING_EN

#endif //TK_SWD_DEBUG_EN
/**
  * @}
  */

/*************************** (C) COPYRIGHT 2022 JUSHENG ***** END OF FILE *****/
